/****************************************************************************
 *  QLyrics - A simple lyrics viewer.                                       *
 *  Copyright (C) 2008  hsys <hanshuiys@gmail.com>                          *
 *                                                                          *
 *  This program is free software; you can redistribute it and/or modify    *
 *  it under the terms of the GNU General Public License as published by    *
 *  the Free Software Foundation; either version 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  This program is distributed in the hope that it will be useful,         *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with this program; if not, write to the Free Software Foundation, *
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA       *
 ****************************************************************************/

#ifndef _LYRICSMANAGER_H_
#define _LYRICSMANAGER_H_

#include "basictype.h"
#include "lyricsprovider.h"
#include "lyricsdata.h"
#include "lyricsjob.h"
#include <vector>

/** This class is used to manager lyrics provider. */
class LyricsManager : public QObject {
    Q_OBJECT
public:
    /** Get the pointer of instance of the class, since the class is a singleton. */
    static void buildInstance(QObject* parent = 0);
    static LyricsManager* getInstance();

    typedef std::vector<LyricsProvider::Builder*>::const_iterator const_iterator;
    const_iterator begin() const { return m_lyricsProvider.begin(); };
    const_iterator end() const { return m_lyricsProvider.end(); };

    /** Search the specified track using all providers. */
    void searchAll(const Track&);
    /** Search the specified track using specific provider. */
    void search(const Track&, const int);

    /** Return current lyrics status. */
    LyricsProvider::LyricsStatus getStatus() const { return m_status; };
    /** Return the instance of lyrics object. */
    LyricsData* getLyrics() { return m_status == LyricsProvider::Success ? &m_lyrics : NULL; };
    const QString& getLyricsFilename() const { return m_filename; };

private:
    std::vector<LyricsProvider::Builder*> m_lyricsProvider;
    LyricsProvider::LyricsStatus m_status;
    QString m_filename;
    LyricsData m_lyrics;
    LyricsJob* m_currentJob;

    LyricsManager(QObject* parent = 0);
    ~LyricsManager();

    static LyricsManager* sm_inst;

    /** Return the file name of downloaded lyrics. */
    const QString& getFilename() const { return m_filename; };

private slots:
    void slotTrackChanged();
    void slotMultiChoice(LyricsJob*);
    void slotJobDone(LyricsJob*);

    /** Reload current lyrics. */
    void reloadLyrics();

signals:
    void signalStatusChanged(LyricsProvider::LyricsStatus);
    void signalMultiChoice(LyricsJob*);
};

#endif
